<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property string $id
 * @property string $username
 * @property string $password
 * @property integer $superuser
 * @property string $avatar
 * @property string $notifyType
 *
 * The followings are the available model relations:
 * @property IdeaProfile[] $ideaProfiles
 * @property IdeaProfileComment[] $ideaProfileComments
 * @property ProjectProfile[] $projectProfiles
 * @property ProjectProfile[] $projectProfiles1
 * @property ProjectProfileComment[] $projectProfileComments
 * @property TeamProfileComment[] $teamProfileComments
 * @property UserProfile[] $userProfiles
 * @property UserProfileComment[] $userProfileComments
 */
class User extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user';
	}
	
	public $password_confirmation;

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, password, password_confirmation', 'required'),
			array('superuser', 'numerical', 'integerOnly'=>true),
			array('username', 'length', 'max'=>20),
			array('password', 'length', 'max'=>128),
			array('password_confirmation', 'length', 'max'=>128),
			array('password', 'compare', 'compareAttribute'=>'password_confirmation'),
			array('avatar', 'length', 'max'=>255),
			array('notifyType', 'length', 'max'=>9),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, username, password, superuser, avatar, notifyType', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'ideaProfiles' => array(self::HAS_MANY, 'IdeaProfile', 'author_id'),
			'ideaProfileComments' => array(self::HAS_MANY, 'IdeaProfileComment', 'user_id'),
			'projectProfiles' => array(self::HAS_MANY, 'ProjectProfile', 'author_id'),
			'projectProfiles1' => array(self::HAS_MANY, 'ProjectProfile', 'supervisor_id'),
			'projectProfileComments' => array(self::HAS_MANY, 'ProjectProfileComment', 'user_id'),
			'teamProfileComments' => array(self::HAS_MANY, 'TeamProfileComment', 'user_id'),
			'userProfiles' => array(self::HAS_MANY, 'UserProfile', 'user_id'),
			'userProfileComments' => array(self::HAS_MANY, 'UserProfileComment', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'username' => 'Username',
			'password' => 'Password',
			'password_confirmation' => 'Password Confirmation',
			'superuser' => 'Superuser',
			'avatar' => 'Avatar',
			'notifyType' => 'Notify Type',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('superuser',$this->superuser);
		$criteria->compare('avatar',$this->avatar,true);
		$criteria->compare('notifyType',$this->notifyType,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

        public function validatePassword($password)
        {
               //return $password===$this->password;   // here pure password are compared without hashing so pure password is stored in database
               //return $this->hashPassword($password,$this->salt)===$this->password;  // here hashed password is stored in database
                return $this->hashPassword($password)===$this->password;  // here hashed password is stored in database
        }

        public function hashPassword($password)
        {
               return md5($password);
        }

        protected function beforeSave()
        {
                if(parent::beforeSave())
                {
                    $this->password = md5($this->password);

                    return true;
                }
                else
                    return false;
        }
}